/*
 * Yet Another Haxe Library, Copyright (c) 2009, Jean-Philippe CIMAN (Jeanffy)
 * All rights reserved.
 * Permission is hereby granted to use, copy, modify, and distribute this
 * source code, or portions hereof, for any purpose, without fee, subject
 * to the following restrictions:
 *
 * 1. The origin of this source code must not be misrepresented.
 * 2. Altered versions must be plainly marked as such and
 *    must not be misrepresented as being the original source.
 * 3. This copyright notice may not be removed or altered from
 *    any source or altered source distribution.
 *
 * If you use this source code in a product, acknowledgment is not
 * required but would be appreciated.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE PROJECT AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
	History:
	v0.2 : porting code to haxe 2.0
	v0.1.1 : added flash9 support
	v0.1 : first version
*/

#if flash8
import flash.MovieClip;
#elseif flash9
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.text.TextFieldAutoSize;
import flash.events.MouseEvent;
#end

/**
	This class implements the start page of a game<br/>
	"Click to play"
**/
class YahClickToPlay
{
	private var m_clip:MovieClip;
	
#if flash9
	private var m_callback:Void->Void;
#end
	
	/**
		parent : clip that will receive newly created clips<br/>
		fontName : the font to use for texts (the library must export at least glyphs required to write "Click to play")<br/>
		depth : depth to create new clip<br/>
		stageW, stageH : stage dimensions<br/>
		startFuction : function that will be called when the user clicks
	**/
	public function new(parent:MovieClip, fontName:String, depth:Int, stageW:Int, stageH:Int, startFunction:Void->Void)
	{
#if flash8
		m_clip = parent.createEmptyMovieClip("yahctp", depth);
		m_clip._x = 0;
		m_clip._y = 0;
		
		var clickText:flash.TextField = m_clip.createTextField("clicktext", 1, 0, 0, stageW, stageH);
		
		var format:flash.TextFormat = new flash.TextFormat();
		format.color = 0x000000;
		format.font = fontName;
		format.size = 20;
		format.align = "center";
		
		clickText.selectable = false;
		clickText.setNewTextFormat(format);
		clickText.text = "Click to play";
		
		var me = this;
		m_clip.onRelease = function()
		{
			startFunction();
		}
#elseif flash9
		m_clip = new MovieClip();
		m_clip.x = 0;
		m_clip.y = 0;
		
		var clickText:TextField = new TextField();
		clickText.embedFonts = true;
		//clickText.autoSize = TextFieldAutoSize.LEFT;
		clickText.width = stageW;
		clickText.height = stageH;
		var format:TextFormat = new TextFormat();
		format.font = fontName;
		format.color = 0xFFFFFF;
		format.size = 30;
		format.align = TextFormatAlign.CENTER;
		clickText.defaultTextFormat = format;
		clickText.text = "Click to play";
		clickText.selectable = false;
		m_clip.addChild(clickText);
		
		m_callback = startFunction;
		m_clip.addEventListener(MouseEvent.CLICK, onRelease);
		
		parent.addChild(m_clip);
#else
	#error
#end
	}
	
#if flash9
	private function onRelease(e:MouseEvent):Void
	{
		m_callback();
	}
#end
	
	public function show()
	{
#if flash8
		m_clip._visible = true;
#elseif flash9
		m_clip.visible = true;
#else
	#error
#end
	}
	
	public function hide()
	{
#if flash8
		m_clip._visible = false;
#elseif flash9
		m_clip.visible = false;
#else
	#error
#end
	}
}